﻿using HZY.Framework.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using SprayProcessSCADASystem.Common;
using SprayProcessSCADASystem.UserControls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LogLevel = Sunny.UI.LogLevel;

namespace SprayProcessSCADASystem.Page
{
    public partial class PageTotalEquipmentControl : UIPage, ISingletonSelfDependency
    {
        private readonly ILogger<PageTotalEquipmentControl> _logger;

        public List<UserDeviceUnitControl> DevUnitControls { get; set; } = new List<UserDeviceUnitControl>();
        public PageTotalEquipmentControl(ILogger<PageTotalEquipmentControl> logger)
        {
            InitializeComponent();
            _logger = logger;
            //GetAllUserDeviceUnitControls();
        }


        

        /*private void GetAllUserDeviceUnitControls()
        {
            foreach (Control item in this.uiTitlePanel2.Controls)
            {
                if (item is UserDeviceUnitControl)
                {
                    UserDeviceUnitControl devUnit = item as UserDeviceUnitControl;
                    DevUnitControls.Add(devUnit);
                }
            }
        }

        private void RefreshDeviceStatus()
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    if (Global.SiemensClient == null || !Global.SiemensClient.Connected) return;
                    for (int i = 0; i < DevUnitControls.Count; i++)
                    {
                        bool devOpen = Convert.ToBoolean(Global.DataDic[DevUnitControls[i].DeviceName + "开"]);
                        bool devClose = Convert.ToBoolean(Global.DataDic[DevUnitControls[i].DeviceName + "关"]);

                        if (devOpen)
                        {
                            this.Invoke(() =>
                            {
                                DevUnitControls[i].State = true;
                            });
                        }

                        if (devClose)
                        {
                            this.Invoke(() =>
                            {
                                DevUnitControls[i].State = false;
                            });
                        }

                    }
                    await Task.Delay(200);
                }
            });

        }*/

        private void btn_common_Click(object sender, EventArgs e)
        {
            UIButton? btn = sender as UIButton;
            if (btn == null) return;

            string varName = btn.TagString;
            if (Global.PlcWrite(varName, true))
            {
                UIMessageBox.ShowSuccess($"{varName}---写入成功！");
                ShowLog($"写入{varName}成功！");
            }
            else
            {
                UIMessageBox.ShowSuccess($"{varName}---写入失败！");
                ShowLog($"写入{varName}失败！");

            }

        }

        //空运行事件
        private void btn_DryRun_Click(object sender, EventArgs e)
        {
            UserCounterButton? btn = sender as UserCounterButton;
            if (btn == null) return;
            if (Global.PlcWrite(btn.VariableName, !btn.CounterButtonState))
            {
                btn.CounterButtonState = !btn.CounterButtonState;
                UIMessageBox.ShowSuccess($"{btn.VariableName}---写入成功！");
                ShowLog($"写入{btn.VariableName}成功！");

            }
            else
            {
                UIMessageBox.ShowError($"{btn.VariableName}--写入失败！");
                ShowLog($"写入{btn.VariableName}失败！");    

            }
        }

        private void Device_common_Click(object sender, EventArgs e)
        {
            UserDeviceUnitControl? device = sender as UserDeviceUnitControl;
            if (device == null) return;
            if (Global.SiemensClient == null || !Global.SiemensClient.Connected)
            {
                device.State = false;
                return;
            }
            var varName = device.State ? device.CloseVariableName : device.OpenVariableName;
            if (Global.PlcWrite(varName, true))
            {
                UIMessageBox.ShowSuccess($"{varName}---写入成功！");
                ShowLog($"写入{varName}成功！");
            }
            else
            {
                UIMessageBox.ShowError($"{varName}---写入失败！");
                ShowLog($"写入{varName}失败！");
            }

        }


        public void ShowLog(string log,LogLevel level = LogLevel.Info)
        {
            this.txt_log.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "=>" + log + Environment.NewLine);
            switch (level)
            {
                case LogLevel.Info:
                    _logger.LogInformation(log);
                    break;
                case LogLevel.Debug:
                    _logger.LogDebug(log);
                    break;
                case LogLevel.Error:
                    _logger.LogError(log);
                    break;
                case LogLevel.Warn: 
                    _logger.LogWarning(log);
                    break;
            }
        }

        
    }


}
